$(function(){
	var curPage = 1;
    var pageSize = 10;
	var favorite = {
		getList: function() {
			$.ajax({
			    url: wsCommon.host + '/api/memberGoods/list',
			    dataType: wsCommon.jsonType,
				data: {
					type: 'fav',
                    pageSize: pageSize
                },
			    success: function(res) {
					if(res.code == 1) {
						if(res.result) {
							var pager = res.data.pager;

							favorite.initPaging(pager.pages, pager.total, res);
						} else {
							wsCommon.setCookie('go_back_link', window.location.href);
							window.location.href = './passport-login.html';
						}
					}
			    },
			    error: function() {
			        // console.log('接口返回失败');
			    }
			})
		},
		randerList: function(pageData) {
			var tpl = '';
			if(pageData.length == 0) {
				tpl += '<tr>\
	                    <td colspan="5" class="no-information">暂无收藏</td>\
	                </tr>';
					$('[data-role="dirPaging"]').hide();
			} else {
				$.each(pageData, function(index, item) {
					var btnTpl = '';
					if(item.store - item.freez > 0) {
						btnTpl = '<a href="javascript:;" class="btn btn-major action-addtocart" data-role="addShopCart" data-goodsid="'+item.goodsId+'" data-productid="'+item.productId+'" data-objtype="'+item.objType+'"><span><span>加入购物车</span></span></a>';
					} else {
							btnTpl = '<a href="javascript:;" class="btn btn-caution action-addtocart" id="notice" data-goodsid="'+item.goodsId+'" data-productid="'+item.productId+'" data-name="'+item.productName+'" data-price="'+item.price+'" data-img="'+item.imageDefaultId+'"><span><span>到货通知</span></span></a>';
					}
					tpl += '<tr class="first">\
							 <td><div class="goods-pic"><a href="./info.html?goodsId='+item.goodsId+'&catId='+item.catId+'" target="_blank"><img src="'+item.imgUrl+'" alt="'+item.productName+'"></a></div></td>\
							 <td class="goods-name"><a href="./info.html?goodsId='+item.goodsId+'&catId='+item.catId+'" target="_blank">'+item.productName+'</a></td>\
							 <td class="price">¥'+item.price+'</td>\
							 <td class="status">'+(item.store - item.freez > 0 ? '有货' : '无货')+'</td>\
							 <td class="actions">'+btnTpl+'<i class="separator"></i><a href="javascript:;" data-role="favDel" data-gnotifyid="'+item.gnotifyId+'">移除</a>\
							  </td>\
						   </tr>';
				})
			}
			$('[data-role="result"]').html(tpl);
		},
		getPageDates: function() {
            var self = this;

            $.ajax({
                url: wsCommon.host + '/api/memberGoods/list',
                dataType: wsCommon.jsonType,
                data: {
					type: 'fav',
					pageNum: curPage,
					pageSize: pageSize
				},
                success: function(res) {

                    // 同步页码
                    var totalPage = res.data.pager.pages;

                    var pageData = res.data.pager.list;
                    var pager = res.data.pager;
                    self.randerList(pageData);
                },
                error: function() {
                    // console.log('分页接口失败');
                }
            });
        },
		initPaging: function(totalPage, totalDataNum, res) {
            var self = this,
                ele = $('[data-role="dirPaging"]');
                if(totalDataNum > 0) {
                    ele.show();
                    var pagination = ele.pagination(totalDataNum, {
                        num_edge_entries: 1,
                        num_display_entries: 4,
                        callback: function(page_index) {
                            if(page_index == 0) {
                                var totalPage = res.data.pager.pages;

                                var pageData = res.data.pager.list;
                                var pager = res.data.pager;
                                favorite.randerList(pageData);
                            } else {
                                curPage = page_index + 1;
                                favorite.getPageDates();
                            }
                        },
                        items_per_page: pageSize,
                        prev_text: "&lt;&lt;上一页",
                        next_text: "下一页&gt;&gt;",
                        link_to: "javascript:;"
                    });

                }else {
                    ele.hide();
                    favorite.randerList([]);
                }
        },
		del: function() {
			$(document).on('click', '[data-role="favDel"]', function() {
				var $this = $(this);
				$.ajax({
	                url: wsCommon.host + '/api/memberGoods/delete',
	                dataType: wsCommon.jsonType,
	                data: {
						gnotifyId: $this.attr('data-gnotifyid')
					},
	                success: function(res) {
						if(res.code == 1) {
							if(res.result) {
								$this.closest('tr').remove();
								iosOverlay({
									text: "删除成功",
									duration: 1000
								});
							} else {
								wsCommon.setCookie('go_back_link', window.location.href);
								window.location.href = './passport-login.html';
							}
						}
	                },
	                error: function() {
	                    // console.log('分页接口失败');
	                }
	            });
			})
		},
		// 到货通知
        arrivalNotice: function() {
            //弹层
    		$(document).on('click',"#notice" ,function(){
    		   $(".cover").show();
    		   $("#notice_pop").show();

    		})
    		$(document).on('click', ".popup-btn-close", function(){
    		   $(this).parents(".popup-container").hide();
    		   $(".cover").hide();
    		})

            // 提交
            $(document).on('click', '[data-role="arrivalNotice"]', function() {
				// var email = $('[data-arrivalNotice="email"]').val().trim();
                var phone = $('[data-arrivalNotice="phone"]').val().trim();
                var phonereg = /^((1[3456789])|(9[28]))\d{9}$/;
                // var emailreg = /^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/;
                if(phone == '') {
                    iosOverlay({
                        text: "手机号码不能为空!",
                        duration: 1500
                    });
                    return;
                } else if(!phonereg.test(phone) && phone != '') {
                    iosOverlay({
                		text: "手机号码格式不正确!",
                        duration: 1500
                	});
                    return;
                } else {
                    var noticeDom = $('#notice');
                    $.ajax({
                        url: wsCommon.host + '/api/memberGoods/save',
                        dataType: wsCommon.jsonType,
                        data: {
                            goodsId: noticeDom.attr('data-goodsid'),
                            productId: noticeDom.attr('data-productid'),
                            goodsName: noticeDom.attr('data-name'),
                            goodsPrice: noticeDom.attr('data-price'),
                            imageDefaultId: noticeDom.attr('data-img'),
                            cellphone: phone
                        },
                        success: function(res) {
                            if(res.code == 1) {
                                iosOverlay({
                                    text: res.reason,
                                    duration: 1500
                                });
                                $(".cover").hide();
                     		    $("#notice_pop").hide();
                            }
                        },
                        error: function() {
                            // console.log('购物车显示接口返回失败');
                        }
                    })
                }
            })
        },
		init: function() {
			this.getList();
			this.del();
			this.arrivalNotice();
		}
	}

	favorite.init();
})
